<!DOCTYPE html>
<html>
<head>
	<title>js缓存计算</title>
</head>
<body>
<script>
	//Memoized
var fibonacci=function(){
  var memo=[0,1];
  var fib = function(n){
    var result=memo[n];
    if(typeof result != 'number'){
      result = fib(n-1) + fib(n-2);
      memo[n]=result;
    }
    return result;
  };
  return fib;
}();

// 或者

var fibArr = [ 0, 1, 1];
(function fibonacci(n){
    var ind = fibArr[n];
    return ind ? ind : (fibArr[n] = fibonacci(n-1) + fibonacci(n-2))
})(50);


//nomemoized
var fib = function(n){
    return n<2 ? n : fib(n-1)+fib(n-2);
}


for(var i=0;i<10;i++){//改成50次差不多炸
console.log(i+" "+fib(i));  
}

for(var i=0;i<10;i++){
console.log(i+" "+fibonacci(i));  
}
</script>
</body>
</html>